package com.mou.controller.user;

import com.mou.dto.CartDTO;
import com.mou.dto.CartQuantityDTO;
import com.mou.entity.Cart;
import com.mou.result.Result;
import com.mou.service.CartService;
import com.mou.vo.CartProductVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController("userCartController")
@RequestMapping("/user/cart")
@Slf4j
public class CartController {
    @Autowired
    private CartService cartService;

    @PostMapping("/add")
    public Result<String> add(@RequestBody CartDTO cartDTO) {
        log.info("购物车新增:{}", cartDTO);
        cartService.add(cartDTO);
        return Result.success();
    }

    @PostMapping("/getCartProduct/{id}")
    public Result<List<CartProductVO>> getCartProduct(@PathVariable("id") Integer id) {
        log.info("购物车商品详情:{}", id);
        List<CartProductVO> cartProductVO = cartService.getCartProduct(id);
        return Result.success(cartProductVO);
    }

    @PutMapping("/update")
    public Result<String> update(@RequestBody CartQuantityDTO cartQuantityDTO) {
        log.info("购物车数量修改:{}", cartQuantityDTO);
        Cart cart = new Cart();
        BeanUtils.copyProperties(cartQuantityDTO, cart);
        cart.setUpdateTime(LocalDate.now());
        cartService.updateById(cart);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable("id") Integer id) {
        log.info("购物车删除:{}", id);
        cartService.removeById(id);
        return Result.success();
    }
}
